﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using System.IO;
using System.Linq;
using System.Threading;

namespace CloudStorageLight.Web.Tests
{

    [TestClass]
    public class SharePointTest
    {
        static IWebDriver driver;
        static string account;

        // クラス内で最初のテストを実行する前に、ClassInitialize を使用してコードを実行してください
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            account = "proaxia";
            driver = SharePointSubScenarioUtil.CreateDriverAndLogin(account);
        }
        //
        // クラス内のテストをすべて実行したら、ClassCleanup を使用してコードを実行してください
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            driver.Close();
        }

        [TestInitialize]
        public void TestSetUp()
        {
            driver.Navigate().GoToUrl(AppUtil.GetAppConfigValue("SharePointSite"));
            try
            {
                SharePointSubScenarioUtil.TryClick(driver, By.LinkText("Cloud Storage Light"));
                SharePointSubScenarioUtil.WaitBusy(driver);
            }
            catch (Exception)
            {
            }
        }

        [TestCleanup]
        public void TestCleanup()
        {
        }


        [TestMethod]
        public void 単純なダウンロード()
        {

            SharePointSubScenarioUtil.ClearDownloadFile("TEST.txt");
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, "TEST.txt");
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile("TEST.txt"));
        }



        [TestMethod]
        public void ZIPダウンロード()
        {

            SharePointSubScenarioUtil.ClearDownloadFile("downloadfiles.zip");
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, "重要.xlsx");
            SharePointSubScenarioUtil.CheckFileElement(driver, "TEST.txt");
            driver.FindElement(By.Id("download-command")).Click();
            driver.FindElement(By.Id("download-zip-command")).Click();

            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile("downloadfiles.zip"));
        }

        [TestMethod]
        public void 削除したファイルの復元()
        {
            var filePath = "UnitTest.txt";
            File.WriteAllText(filePath, "TEST");
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();

            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));
            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/", account);
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            SharePointSubScenarioUtil.RemoveFile(driver, filePath);

            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/", account);
            var deleted = SubScenarioUtil.Exists(driver, filePath);
            Assert.IsTrue(!deleted);

            for (int i=0; i<5; i++)
            {
                try
                {
                    SharePointSubScenarioUtil.ClickMenuCommand(driver, "list-deleted-command");
                    SharePointSubScenarioUtil.CheckFileElement(driver, "UnitTest.txt");
                    SharePointSubScenarioUtil.ClickMenuCommand(driver, "recover-command");
                    break;
                }
                catch (Exception)
                {

                    if (i==4) throw;
                }

            }

            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/", account);

            var recoveried = SharePointSubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            Assert.IsTrue(recoveried != null);
        }

        [TestMethod]
        public void 履歴表示()
        {
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);

            SharePointSubScenarioUtil.ClickContextMenu(driver, "TEST.txt", "history-context-command");
            SharePointSubScenarioUtil.TryFind(driver, By.ClassName("icon-history"));
            Assert.IsTrue(driver.FindElements(By.ClassName("icon-history")).Count > 0);
        }

        [TestMethod]
        public void アクセスログ()
        {
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);

            SharePointSubScenarioUtil.ClickContextMenu(driver, "TEST.txt", "accesslog-context-command");
            var table = SharePointSubScenarioUtil.TryFind(driver, By.XPath("//table[@class='table']/tbody/tr"), 120);
            Assert.IsNotNull(table);
        }


        [TestMethod]
        public void 単純な検索()
        {
            //WAIT FOR BUSY
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo");

            SharePointSubScenarioUtil.TryFind(driver, By.Id("query")).SendKeys("テスト");
            SharePointSubScenarioUtil.TryFind(driver, By.Id("search")).Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, "テストデータ.pptx");
            SharePointSubScenarioUtil.ClearDownloadFile("テストデータ.pptx");
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile("テストデータ.pptx"));
        }

        [TestMethod]
        public void 単純なアップロード()
        {
            var filePath = "UnitTest.txt";
            File.WriteAllText(filePath, "TEST");
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);

            SharePointSubScenarioUtil.DeleteFile(driver, filePath);
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));

            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/", account);
            SharePointSubScenarioUtil.WaitBusy(driver);
            var uploaded = SharePointSubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            Assert.IsTrue(uploaded != null);
        }


        [TestMethod]
        public void パスワードテキストのダウンロードとアップロード()
        {
            var filename = "a.txt";
            var filename2 = "a.txt.zip";
            SharePointSubScenarioUtil.ClearDownloadFile(filename2);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Password").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, filename);
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile(filename2));
        }


        [TestMethod]
        public void パスワードOffice文書のダウンロードとアップロード()
        {
            var filename = "TEST.pptx";
            SharePointSubScenarioUtil.ClearDownloadFile(filename);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Password").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, filename);
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile(filename));
            SharePointSubScenarioUtil.RemoveFile(driver, filename);
            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filename));
            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/Password/", account);
            SharePointSubScenarioUtil.WaitBusy(driver);
            var uploaded = SharePointSubScenarioUtil.GetFolderOrFileElement(driver, filename);
        }

        [TestMethod]
        public void RMSテキストのダウンロードとアップロード()
        {
            var filename = "a.txt";
            var filename2 = "a.ptxt";
            SharePointSubScenarioUtil.ClearDownloadFile(filename2);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "RMS").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, filename);
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile(filename2));
        }

        [TestMethod]
        public void RMSOffice文書のダウンロードとアップロード()
        {
            var filename = "TEST.pptx";
            var filename2 = "TEST.pptx";
            SharePointSubScenarioUtil.ClearDownloadFile(filename2);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "RMS").Click();
            SharePointSubScenarioUtil.CheckFileElement(driver, filename);
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile(filename2));
            SharePointSubScenarioUtil.RemoveFile(driver, filename);
            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filename2));
            SharePointSubScenarioUtil.Navigate(driver, "f/Demo/RMS/", account);
            SharePointSubScenarioUtil.WaitBusy(driver);
            var uploaded = SharePointSubScenarioUtil.GetFolderOrFileElement(driver, filename);
        }

        [TestMethod]
        public void フォルダ作成()
        {
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);
            if (SharePointSubScenarioUtil.Exists(driver, "単体テストFolder"))
            {
                SharePointSubScenarioUtil.DeleteFolder(driver, "単体テストFolder");
                SharePointSubScenarioUtil.WaitBusy(driver);
            }
            SharePointSubScenarioUtil.ClickMenuCommand(driver, "newfolder-command");
            driver.FindElement(By.Id("newFolderName")).SendKeys("単体テストFolder");
            driver.FindElement(By.Id("create-newFolder")).Click();
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "単体テストFolder").Click();
        }

        [TestMethod]
        public void フォルダ設定()
        {
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Sub").Click();
            SharePointSubScenarioUtil.ClickMenuCommand(driver, "folder-setup-command");
            var ele = SharePointSubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__Role"), 30);
            var ch = driver.FindElements(By.Id("AccessTokens_0__InUse"));
            if (ch.Count > 0)
            {
                ch[0].Click();
                SharePointSubScenarioUtil.TryClick(driver, By.Id("blobItemDetailOkButton"));
            }
            SharePointSubScenarioUtil.ClickMenuCommand(driver, "folder-setup-command");
            ele = SharePointSubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__Role"), 30);
            for (int i=0; i<10; i++)
            {
                try
                {
                    ele.SendKeys("sales");
                    break;
                }
                catch (Exception)
                {
                }
                Thread.Sleep(2000);

            }
            ele = SharePointSubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__CanRead"));
            ele.Click();
            SharePointSubScenarioUtil.TryClick(driver, By.Id("blobItemDetailOkButton"));
            var subFolder = SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Sub10");
            Assert.IsTrue(subFolder != null);
            Thread.Sleep(3000);
        }

        [TestMethod]
        public void ファイル送信()
        {
            var email = AppUtil.GetAppConfigValue("TestEmail");
            var pass = AppUtil.GetAppConfigValue("TestPassword");
            var titleString = "自動テスト" + Guid.NewGuid().ToString();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.CheckFileElement(driver, "TEST.txt");
            SharePointSubScenarioUtil.CheckFileElement(driver, "重要.xlsx");
            SharePointSubScenarioUtil.ClickMenuCommand(driver, "transfer-file-command");

            var to = SharePointSubScenarioUtil.TryFind(driver, By.Id("To"));
            to.SendKeys(email);
            var title = driver.FindElement(By.Id("Title"));
            title.SendKeys(titleString);
            var password = driver.FindElement(By.Id("Password"));
            password.SendKeys(pass);
            driver.FindElement(By.Id("sendButton")).Click();

            string guid = null;
            for (int i = 0; i < 10; i++)
            {
                guid = SharePointSubScenarioUtil.GetFileTransferGuid(account, titleString);
                if (guid != null) break;
                Thread.Sleep(1000);
            }

            using (var driver2 = SharePointSubScenarioUtil.CreateWebDriver())
            {
                SharePointSubScenarioUtil.WaitBusy(driver2);
                SharePointSubScenarioUtil.Navigate(driver2, "t/FileTransfer/" + guid, account);

                var mailAddress2 = SharePointSubScenarioUtil.TryFind(driver2, By.Id("mailAddress"));
                var password2 = SharePointSubScenarioUtil.TryFind(driver2, By.Id("password"));

                mailAddress2.SendKeys(email);
                password2.SendKeys(pass);
                driver2.FindElement(By.Id("login")).Click();
                SharePointSubScenarioUtil.CheckFileElement(driver2, "TEST.txt");
                SharePointSubScenarioUtil.CheckFileElement(driver2, "重要.xlsx");

                driver2.FindElement(By.Id("download-command")).Click();

                SharePointSubScenarioUtil.ClearDownloadFile("downloadfiles.zip");

                driver2.FindElement(By.Id("download-zip-file-transfer-command")).Click();
                Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile("downloadfiles.zip"));


            }
        }

        [TestMethod]
        public void ファイル受信()
        {
            var email = AppUtil.GetAppConfigValue("TestEmail");
            var pass = AppUtil.GetAppConfigValue("TestPassword");
            var titleString = "自動テスト" + Guid.NewGuid().ToString();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.CheckFileElement(driver, "TEST.txt");
            SharePointSubScenarioUtil.ClickMenuCommand(driver, "transfer-file-command");

            var to = SharePointSubScenarioUtil.TryFind(driver, By.Id("To"));
            to.SendKeys(email);
            var title = driver.FindElement(By.Id("Title"));
            title.SendKeys(titleString);
            var fileReciveMode = driver.FindElement(By.Id("FileReciveMode"));
            fileReciveMode.FindElement(By.XPath("./option[2]")).Click();
            driver.FindElement(By.Id("sendButton")).Click();

            string guid = null;
            for (int i = 0; i < 10; i++)
            {
                guid = SubScenarioUtil.GetFileTransferGuid(account, titleString);
                Thread.Sleep(1000);
                if (guid != null) break;
            }
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.Navigate(driver, "t/FileTransfer/" + guid, account);

            var filePath = "UnitTest.txt";
            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));


            SharePointSubScenarioUtil.Navigate(driver, "t/FileTransfer/" + guid, account);
            var uploadedlist = driver.FindElements(By.XPath(".//span[@class='item-name']"));
            Assert.IsTrue(uploadedlist.Any(x => x.Text == filePath));
        }

        [TestMethod]
        public void Webパーツ()
        {
            SharePointSubScenarioUtil.WaitBusy(driver);
            SharePointSubScenarioUtil.Navigate(driver, "w/?SPHostUrl=https%3A%2F%2Fproaxiaconsulting.sharepoint.com&initPath=%2F&pageSize=5&showHeader=true", account);


            SharePointSubScenarioUtil.ClearDownloadFile("TEST.txt");
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SharePointSubScenarioUtil.Navigate(driver, "w/Demo/TEST.txt?SPHostUrl=https%3A%2F%2Fproaxiaconsulting.sharepoint.com&", account);
            Assert.IsTrue(SharePointSubScenarioUtil.CheckDownloadFile("TEST.txt"));

            var filePath = "UnitTest.txt";
            File.WriteAllText(filePath, "TEST");
            SharePointSubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));

            SharePointSubScenarioUtil.Navigate(driver, "w/?SPHostUrl=https%3A%2F%2Fproaxiaconsulting.sharepoint.com&initPath=%2F&pageSize=5&showHeader=true", account);
            SharePointSubScenarioUtil.WaitBusy(driver);
            for (int i=0; i<5; i++)
            {
                try
                {
                    SharePointSubScenarioUtil.TryFind(driver, By.Id("query")).SendKeys("テスト");
                    break;
                }
                catch (Exception)
                {

                    if (i==4) throw;
                    Thread.Sleep(1000);
                }

            }
            SharePointSubScenarioUtil.TryFind(driver, By.Id("search")).Click();
            SharePointSubScenarioUtil.GetFolderOrFileElement(driver, "テストデータ.pptx").Click();
        }

    }
}
